package com.gt.app.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.domain.EsScreenProject;
import com.gt.app.domain.EsScreenProjectUser;
import com.gt.app.domain.dto.EsScreenProjectDto;
import com.gt.app.mapper.EsScreenProjectMapper;
import com.gt.app.service.EsScreenProjectService;
import com.gt.app.service.EsScreenProjectUserService;
import com.gt.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 筛查项目表 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsScreenProjectServiceImpl extends ServiceImpl<EsScreenProjectMapper, EsScreenProject> implements EsScreenProjectService {

    @Autowired
    private EsScreenProjectUserService esScreenProjectUserService;

    @Override
    public List<EsScreenProjectDto> getStatus() {
        ArrayList<EsScreenProjectDto> esScreenProjectDtoList = new ArrayList<>();
        List<EsScreenProject> esScreenProjects = this.list();
        LambdaQueryWrapper<EsScreenProjectUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EsScreenProjectUser::getUserId, SecurityUtils.getUserId());
        List<EsScreenProjectUser> esScreenProjectUsers = esScreenProjectUserService.list(queryWrapper);
        if (esScreenProjects != null) {
            for (EsScreenProject esScreenProject : esScreenProjects) {
                EsScreenProjectDto esScreenProjectDto = new EsScreenProjectDto();
                BeanUtils.copyProperties(esScreenProject, esScreenProjectDto);

                String[] diseaseNames = esScreenProject.getDiseaseName().split("；");
                esScreenProjectDto.setDiseaseNames(diseaseNames);
                esScreenProjectDto.setStatus("0");
                if (esScreenProjectUsers != null) {
                    for (EsScreenProjectUser esScreenProjectUser : esScreenProjectUsers) {
                        if (esScreenProjectUser.getUserId() != null && esScreenProject.getId().equals(esScreenProjectUser.getProjectId())) {
                            esScreenProjectDto.setStatus("1");
                            break;
                        }
                    }
                }
                esScreenProjectDtoList.add(esScreenProjectDto);
            }

        }

        return esScreenProjectDtoList;
    }

    @Override
    public String getCheckId(Long id) {
        if (id == null){
            return null;
        }
        LambdaQueryWrapper<EsScreenProjectUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EsScreenProjectUser::getUserId,SecurityUtils.getUserId());
        wrapper.eq(EsScreenProjectUser::getProjectId,id);
        EsScreenProjectUser projectUser = esScreenProjectUserService.getOne(wrapper);
        if (projectUser != null){
            return projectUser.getEsCheckId();
        }
        String checkId = IdUtil.getSnowflake(1L, 1L).nextIdStr();
        EsScreenProjectUser esScreenProjectUser = new EsScreenProjectUser();
        esScreenProjectUser.setProjectId(id);
        esScreenProjectUser.setEsCheckId(checkId);
        esScreenProjectUser.setUserId(SecurityUtils.getUserId());
        boolean save = esScreenProjectUserService.save(esScreenProjectUser);
        if (save) {
            return checkId;
        }
        return null;
    }

    @Override
    public List<EsScreenProjectDto> getScreenRecord() {
        /**此处相当查询两张表的连接查询**/
        List<EsScreenProjectDto> screenProjectDtoList = new ArrayList<>();
        List<EsScreenProject> screenProjects = this.list();
        LambdaQueryWrapper<EsScreenProjectUser> wrapper = new LambdaQueryWrapper<EsScreenProjectUser>()
                .eq(EsScreenProjectUser::getUserId, SecurityUtils.getUserId());
        List<EsScreenProjectUser> screenProjectUsers = esScreenProjectUserService.list(wrapper);
        if (screenProjectUsers == null) {
            return null;
        }
        for (EsScreenProjectUser screenProjectUser : screenProjectUsers) {
            EsScreenProjectDto esScreenProjectDto = new EsScreenProjectDto();
            for (EsScreenProject screenProject : screenProjects) {
                if (screenProjectUser.getProjectId().equals(screenProject.getId())){
                    BeanUtils.copyProperties(screenProject,esScreenProjectDto);
                    String[] diseaseNames = screenProject.getDiseaseName().split("；");
                    esScreenProjectDto.setDiseaseNames(diseaseNames);
                }
            }
            esScreenProjectDto.setStatus("1");
            screenProjectDtoList.add(esScreenProjectDto);
        }
        return screenProjectDtoList;
    }
}
